package com.hrpc.config;
import com.hrpc.IdGenerate;
import com.hrpc.ProtocalConfig;
import com.hrpc.compress.Compressor;
import com.hrpc.compress.impl.GzipCompressorImpl;
import com.hrpc.discovery.RegisterConfig;
import com.hrpc.loadbalancer.LoadBalancer;
import com.hrpc.loadbalancer.impl.RoundRobinLoadBalancer;
import com.hrpc.protection.CircuitBreaker;
import com.hrpc.protection.RateLimiter;
import com.hrpc.protection.TokenBuketRateLimiter;
import com.hrpc.serialize.Serialize;
import com.hrpc.serialize.impl.JDKSerializeImpl;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.*;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 全局配置类：代码配置 ---> XML配置---->默认项
 */
public class Configuration {
    //配置信息--->端口号
    private int port = 8088;
    //配置信息--->应用程序的名字
    private String appName="default";
    //配置信息--->配置注册中心
    private RegisterConfig registerConfig=new RegisterConfig("zookeeper://127.0.0.1:2181");
    //配置信息--->配置序列化协议
    private ProtocalConfig protocalConfig=new ProtocalConfig("jdk");
    //配置信息--->ID生成器
    private IdGenerate id_generate=new IdGenerate(1,2);
    //配置信息--->序列化协议
    private  String serializeType="jdk";
    //配置信息--->压缩使用的协议
    private String compressType="gzip";

    //配置信息--->负载均衡策略
    private LoadBalancer load_balancer=new RoundRobinLoadBalancer();
    //为每一个IP配置一个限流器
    private final Map<SocketAddress, RateLimiter> everyIpRateLimiter=new ConcurrentHashMap<>(16);
    //为每一个IP配置一个断路器
    private Map<SocketAddress, CircuitBreaker> everyIpCircuitBreaker=new ConcurrentHashMap<>(16);
    //分组信息
    private String group="default";


    //读XML
    public Configuration(){
        //1.成员变量的默认配置项

        //1.spi机制发现相关配置项
        SpiReslover spiReslover=new SpiReslover();
        spiReslover.loadFromSpi(this);
        //2.读取xml获得上面的信息
        XmlReslover xmlReslover=new XmlReslover();
        xmlReslover.loadFromXml(this);
        //4.配置信息，hRpcBootStrap提供
    }


    //进行配置


    public Map<SocketAddress, CircuitBreaker> getEveryIpCircuitBreaker() {
        return everyIpCircuitBreaker;
    }

    public void setEveryIpCircuitBreaker(Map<SocketAddress, CircuitBreaker> everyIpCircuitBreaker) {
        this.everyIpCircuitBreaker = everyIpCircuitBreaker;
    }

    public Map<SocketAddress, RateLimiter> getEveryIpRateLimiter() {
        return everyIpRateLimiter;
    }



    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public RegisterConfig getRegisterConfig() {
        return registerConfig;
    }

    public void setRegisterConfig(RegisterConfig registerConfig) {
        this.registerConfig = registerConfig;
    }

    public ProtocalConfig getProtocalConfig() {
        return protocalConfig;
    }

    public void setProtocalConfig(ProtocalConfig protocalConfig) {
        this.protocalConfig = protocalConfig;
    }

    public IdGenerate getId_generate() {
        return id_generate;
    }

    public void setId_generate(IdGenerate id_generate) {
        this.id_generate = id_generate;
    }

    public String getSerializeType() {
        return serializeType;
    }

    public void setSerializeType(String serializeType) {
        this.serializeType = serializeType;
    }

    public String getCompressType() {
        return compressType;
    }

    public void setCompressType(String compressType) {
        this.compressType = compressType;
    }


    public LoadBalancer getLoad_balancer() {
        return load_balancer;
    }

    public void  setLoad_balancer(LoadBalancer load_balancer) {
        this.load_balancer = load_balancer;
    }

    public static void main(String[] args) {
        Configuration configuration=new Configuration();
    }
}
